if select(2, UnitClass("player")) ~= "SHAMAN" then
	-- Non-shamans suck and cannot stand the awesomeness of this module.
	return
end

local aUF = LibStub("AceAddon-3.0"):GetAddon("ag_UnitFrames")
local plugin = aUF:NewModule("Totems")
local newFrame, delFrame = aUF.newFrame, aUF.delFrame

plugin.inherit = "player"

function plugin:OnRegister()
	self:RegisterDefaults({
		units = {
			["player"] = {
				Totems = true,
				TotemTooltips = true,
			},
		}
	})
	self.subConfig = "Bars"
end


function plugin:OnRegisterEvents(object, power)
	if not power then
		object:RegisterEvent("PLAYER_TOTEM_UPDATE", self.UpdateAllTotems)
		object:RegisterEvent("PLAYER_ENTERING_WORLD", self.UpdateAllTotems)
	else
		object:UnregisterEvent("PLAYER_TOTEM_UPDATE", self.UpdateAllTotems)
		object:UnregisterEvent("PLAYER_ENTERING_WORLD", self.UpdateAllTotems)
	end
end

local function onclick(frame, mousebutton)
	if mousebutton == "RightButton" then
		DestroyTotem(this.slot)
	end
end

local function onenter()
	GameTooltip_SetDefaultAnchor(GameTooltip, this)
	GameTooltip:SetTotem(this.slot)
end

local function onleave()
	GameTooltip:Hide()
end

function plugin:OnObjectEnable(object)
	if not plugin.db.profile.units[object.type].Totems then return end
	if not object.Totems then
		object.Totems = newFrame("Texture", object.frame, "BORDER")
		object.Totems:SetTexture(0, 0, 0, 1)
		object.Totems:SetVertexColor(0.25, 0.25, 0.25, 0)
		object.Totems:SetWidth((MAX_TOTEMS * 15) + 4)
		object.Totems:SetHeight(19)
		object.Totems:Show()
		for i=1,MAX_TOTEMS do
			local name = "Totem" .. i
			local frame = newFrame("Button", object.middle, "OVERLAY")
			frame:SetHeight(15)
			frame:SetWidth(15)
			frame:Hide()
			frame.slot = i
			
			frame.Icon = newFrame("Texture", frame, "BACKGROUND")
			frame.Icon:SetAllPoints(frame)

			frame.Cooldown = newFrame("cooldown", frame)
			frame.Cooldown:Hide()
			frame.Cooldown:SetAllPoints(frame)
			frame.Cooldown:SetReverse(true)
			frame.Cooldown:SetFrameLevel(frame:GetFrameLevel())
			frame.Cooldown:SetAlpha(0.8)
			
			object.Totems[name] = frame
			
			if i > 1 then
				frame:SetPoint("BOTTOMLEFT", object.Totems["Totem"..i-1], "BOTTOMRIGHT", 2, 0)
			else
				frame:SetPoint("BOTTOMLEFT", object.Totems,"BOTTOMLEFT", 2, 2)
			end
		end
	end
end

function plugin:OnObjectDisable(object)
	if object.Totems then
		for i=1,MAX_TOTEMS do
			local name = "Totem" .. i
			local frame = object.Totems[name]
			frame.Icon = delFrame(frame.Icon)
			frame.Cooldown = delFrame(frame.cooldown)
			object.Totems[name] = delFrame(frame)
		end
		object.Totems = delFrame(object.Totems)
		self:OnRegisterEvents(object, true)
	end
end

function plugin:OnDisable(object)
	for _,object in aUF:IterateUnitObjects() do
		self:OnObjectDisable(object)
	end
end

function plugin:OnEnable()
	for _,object in aUF:IterateUnitObjects() do
		if object.type and object.inherit == self.inherit and plugin.db.profile.units[object.type].Totems then
			self:OnObjectEnable(object)
			if object.unit then
				self.UpdateAllTotems(object)
				self:OnRegisterEvents(object)
			end
		end
	end
end

function plugin:OnUpdateAll(object)
	local tooltips = plugin.db.profile.units[object.type].TotemTooltips
	for i=1,MAX_TOTEMS do
		local frame = object.Totems["Totem"..i]
		frame:RegisterForClicks("RightButtonUp")
		frame:SetScript("OnClick", onclick)
		frame:SetScript("OnEnter", tooltips and onenter or nil)
		frame:SetScript("OnLeave", tooltips and onleave or nil)
	end
	self.UpdateAllTotems(object)
end
local setupMode = false
function plugin:OnUpdateSetupMode(object, flag)
	setupMode = flag
	if flag then 
		plugin.ShowTotemPosition(object) 
	else
		plugin.UpdateAllTotems(object)
	end
end

function plugin.ShowTotemPosition(self)
	if not self.Totems then return end
	for slot=1, MAX_TOTEMS do
		local frame = self.Totems["Totem"..slot]
		frame.Icon:SetTexture("Interface\\Icons\\Spell_Totem_WardOfDraining")
		frame:Show();
	end
end
-- Unit methods --

function plugin.UpdateTotem(self, slot)
	if not self.Totems then return end
	if setupMode then return end
	local frame = self.Totems["Totem"..slot]	
	local haveTotem, name, startTime, duration, icon = GetTotemInfo(slot)
	if haveTotem and name ~= "" then
		frame.Icon:SetTexture(icon)
		frame.Cooldown:SetCooldown(startTime, duration)
		frame:Show()
	else
		frame:Hide()
	end
end

function plugin.UpdateAllTotems(self)
	for i=1, MAX_TOTEMS do
		plugin.UpdateTotem(self, i)
	end
end
